<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :okText="$t('保存')"
    :cancelText="$t('关闭')"
    wrapClassName="ant-modal-cust-warp"
    style="top:5%;height: 85%;overflow-y: hidden">

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form"  v-bind="layout"  :model="model" :rules="validatorRules">
        <a-form-model-item :label="$t('类型编码')" required prop="code">
          <a-input v-model="model.code" :disabled="roleDisabled"  :placeholder="$t('请输入类型编码')"/>
        </a-form-model-item>
        <a-form-model-item :label="$t('类型名称')" required prop="name">
          <a-input v-model="model.name" :placeholder="$t('请输入类型名称')"/>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
  import {addRole,editRole,duplicateCheck } from '@/api/api'
  import {  postAction } from '@/api/manage'
  export default {
    name: "RoleModal",
    data () {
      return {
        title:this.$t('操作'),
        visible: false,
        roleDisabled: false,
        model: {},
        layout: {
          labelCol: { span: 3 },
          wrapperCol: { span: 14 },
        },
        confirmLoading: false,
        validatorRules:{
          code: [
            { required: true, message: this.$t('请输入类型编码') },
            { validator: this.validateRoleCode}
          ],
          name: [
            { required: true, message: this.$t('请输入类型名称')},
          ],
        },
        url:{
          add:'/system/bisSysType/add',
          edit:'/system/bisSysType/edit'
        }
      }
    },
    created () {
      //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        console.log(record)
        this.model = Object.assign({}, record);
        this.visible = true;
        //编辑页面禁止修改角色编码
        if(this.model.id){
          this.roleDisabled = true;
        }else{
          this.roleDisabled = false;
        }
      },
      close () {
        this.$refs.form.clearValidate();
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let url = ''
            if(!this.model.id){
              url = this.url.add
            }else{
              url = this.url.edit
            }
            postAction(url,this.model).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }else{
            return false;
          }
        })
      },
      handleCancel () {
        this.close()
      },
      validateRoleCode(rule, value, callback){
        if(/[\u4E00-\u9FA5]/g.test(value)){
          callback(this.$t('编码不可输入汉字'));
        }else{
          let params = {
            tableName: "bis_sys_type",
            fieldName: "code",
            fieldVal: value,
            dataId: this.model.id,
          };
          duplicateCheck(params).then((res)=>{
            if(res.success){
              callback();
            }else{
              callback(res.message);
            }
          });
        }
      }
    }
  }
</script>

<style scoped>

</style>